package com.github.byakkili.bim.core.threadpool;

import cn.hutool.core.thread.NamedThreadFactory;
import com.github.byakkili.bim.core.Constants;
import com.github.byakkili.bim.core.threadpool.support.AbortPolicyWithReport;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Guannian Li
 */
@Setter
@Getter
public class CachedThreadPool extends AbstractThreadPool {
    private String name = Constants.DEFAULT_THREAD_NAME;
    private int cores = Constants.DEFAULT_CORE_THREADS;
    private int threads = Integer.MAX_VALUE;
    private int queues = Constants.DEFAULT_QUEUES;
    private int alive = 60_000;
    private String dumpPath = null;

    @Override
    public ExecutorService getExecutor() {
        return new ThreadPoolExecutor(
                cores, threads,
                alive, TimeUnit.MILLISECONDS,
                workQueue(queues),
                new NamedThreadFactory(name, false),
                new AbortPolicyWithReport(name, dumpPath));
    }
}
